package com.garlic.face.jlf.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.garlic.face.jlf.R;
import com.garlic.face.jlf.transaction.bean.SaleFlow;
import com.garlic.face.jlf.utils.Tools;

import java.util.List;

public class GoodsRecyclerViewAdapter extends RecyclerView.Adapter<GoodsRecyclerViewAdapter.GoodsItemViewHolder> {
    private List<SaleFlow> mList;
    private Context mContext;
    private OnItemListener mOnItemListener;
    private CallBack mCallBack = null;

    public GoodsRecyclerViewAdapter(Context ctx, List<SaleFlow> list) {
        mContext = ctx;
        this.mList = list;
    }

    void setData(List<SaleFlow> list) {
        mList = list;
        super.notifyDataSetChanged();
    }

    void setCallBack(CallBack callBack){
        this.mCallBack = callBack;
    }

    interface CallBack{
        void clickNumber(int pos);
    }

    @Override
    public GoodsItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, parent, false);
        GoodsItemViewHolder viewHolder = new GoodsItemViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(GoodsItemViewHolder holder, int position) {
        final int pos = position;
        final SaleFlow item = mList.get(position);
        holder.nameText.setText(""+item.getFname());
        String realMoney = "¥" + Tools.getFormatNumber(item.getPrice());
        holder.realMoneyText.setText(realMoney);
        String totalMoney = String.format(mContext.getResources().getString(R.string.goods_item_total_money),Tools.getFormatNumber(item.getUnitprice()));
//        SpannableString spannableString = new SpannableString(totalMoney);
//        spannableString.setSpan(new StrikethroughSpan(),3, totalMoney.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
        holder.totalMoneyText.setText(totalMoney);
        holder.totalMoneyText.setPaintFlags(holder.totalMoneyText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

        if(item.getPlutype().equals("3")){
            holder.plutype3barcodeText.setText(item.getBarcode().substring(1, item.getBarcode().length()));
            holder.plutype0barcodeText.setText("");
        }else {
            holder.plutype0barcodeText.setText(item.getBarcode().substring(1, item.getBarcode().length()));
            holder.plutype3barcodeText.setText("");
        }

        double count = item.getCount();
        holder.numberText.setText(String.valueOf((int) count));
        if (item.getPrice() == item.getUnitprice()) {
            holder.totalMoneyText.setVisibility(View.GONE);
        } else {
            holder.totalMoneyText.setVisibility(View.VISIBLE);
        }

        holder.goodsItemCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemListener != null) {
                    mOnItemListener.onItemDelete(pos);
                }
            }
        });

        holder.numberText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mCallBack != null){
                    mCallBack.clickNumber(pos);
                }
            }
        });

        if(item.isHighlight()){
            holder.item_container.setBackgroundColor(Color.parseColor("#A2D8FF"));
        }else {
            holder.item_container.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }

        //todo 商品总数量获取接口不对
        holder.realMoneyText.setVisibility(View.VISIBLE);
//        if ("1".equals(item.getPlutype()) || "2".equals(item.getPlutype())) {
        if ("1".equals(item.getPlutype())) {
            if(item.getWeightPluType() == 2){
                holder.goodsItemAdd.setVisibility(View.VISIBLE);
                holder.goodsItemReduce.setVisibility(View.VISIBLE);
                holder.numberText.setVisibility(View.VISIBLE);
                holder.numberText.setBackgroundResource(R.drawable.bg_tv_goods_count);
                holder.numberText.setPadding(6, 0, 6, 0);
            }else {
                holder.numberText.setVisibility(View.VISIBLE);
                holder.numberText.setClickable(false);
                if(item.getCount() == 1){
                    holder.numberText.setText("");
                }
                holder.numberText.setBackgroundResource(0);
                holder.goodsItemAdd.setVisibility(View.INVISIBLE);
                holder.goodsItemReduce.setVisibility(View.INVISIBLE);
            }
        } else if("3".equals(item.getPlutype())){
            holder.goodsItemAdd.setVisibility(View.GONE);
            holder.goodsItemReduce.setVisibility(View.GONE);
            holder.numberText.setVisibility(View.GONE);
            holder.realMoneyText.setVisibility(View.GONE);
        }else {
            holder.goodsItemAdd.setVisibility(View.VISIBLE);
            holder.goodsItemReduce.setVisibility(View.VISIBLE);
            holder.numberText.setVisibility(View.VISIBLE);
            holder.numberText.setBackgroundResource(R.drawable.bg_tv_goods_count);
            holder.numberText.setPadding(6, 0, 6, 0);
        }
        holder.goodsItemAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemListener != null) {
                    mOnItemListener.onItemAdd(pos);
                }
            }
        });

        holder.goodsItemReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getCount() <= 1) {
                    return;
                }
                if (mOnItemListener != null) {
                    mOnItemListener.onItemMiuns(pos);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setOnItemListener(OnItemListener l) {
        mOnItemListener = l;
    }

    interface OnItemListener {
        void onItemDelete(int position);

        void onItemAdd(int position);

        void onItemMiuns(int position);
    }

    class GoodsItemViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView numberText;
        TextView realMoneyText;
        TextView totalMoneyText;
        ImageView addImg;
        ImageView reduceImg;
        ImageView goodsItemCancel;
        ImageView goodsItemReduce;
        ImageView goodsItemAdd;
        TextView plutype0barcodeText;
        TextView plutype3barcodeText;
        RelativeLayout item_container;

        GoodsItemViewHolder(View itemView) {
            super(itemView);
            item_container = itemView.findViewById(R.id.item_container);
            nameText = itemView.findViewById(R.id.goods_item_name);
            realMoneyText = itemView.findViewById(R.id.goods_item_real_money_tv);
            totalMoneyText = itemView.findViewById(R.id.goods_item_total_money);
            numberText = itemView.findViewById(R.id.goods_item_number);
            addImg = itemView.findViewById(R.id.goods_item_add_icon);
            reduceImg = itemView.findViewById(R.id.goods_item_reduce_icon);
            goodsItemCancel = itemView.findViewById(R.id.goods_item_cancel);
            goodsItemReduce = itemView.findViewById(R.id.goods_item_reduce_icon);
            goodsItemAdd = itemView.findViewById(R.id.goods_item_add_icon);
            plutype0barcodeText = itemView.findViewById(R.id.tv_plutype_0_barcode);
            plutype3barcodeText = itemView.findViewById(R.id.tv_plutype_3_barcode);
        }
    }
}
